import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

pd.set_option('display.max_columns', None)
df_trade = pd.read_csv('tianchi_mum_baby_trade_history.csv')

df_baby = pd.read_csv('tianchi_mum_baby.csv')
df_trade['day'] = pd.to_datetime(df_trade.day.astype('str'))

df_trade['year'] = df_trade.day.dt.year
df_trade['quarter'] = df_trade.day.dt.quarter
df_trade['month'] = df_trade.day.dt.month

# print(df_trade.buy_mount.describe())
#
# df_trade = df_trade[df_trade.buy_mount < 189]
# print(df_trade.buy_mount.describe())
#
# print('客户表的数量:', df_baby.__len__())
#
# df_baby = df_baby[df_baby.gender != 2]
# print('客户表的数量:', df_baby.__len__())
#
# df_baby['birthday'] = pd.to_datetime(df_baby.birthday.astype('str'))
#
# df_baby['year'] = df_baby.birthday.dt.year
# df_baby['quarter'] = df_baby.birthday.dt.quarter
# df_baby['month'] = df_baby.birthday.dt.month

# print('婴儿出生年的描述')
# df_baby = df_baby[df_baby.birthday > '2010-01-01']
# df_baby.birthday.describe()
# print(df_baby.year.describe())
#
# print(df_baby.birthday.dt.day)

# 每年的销售量
year_stats = df_trade.groupby(by=['year'])['buy_mount'].sum()  # select year, sum(buy_mount) from df_trade group by year
print(type(year_stats))
print(year_stats)

plt.figure(figsize=(10, 5))
plt.bar(year_stats.index, year_stats.values)
plt.title("Sales Volume By Year")
plt.xlabel("Year")
plt.ylabel("Sales Volumn")
#
# quarter_sales = {}  # keys: 年份_季度， value: 销售总量
#
# for index, row in df_trade.iterrows():
#     k = str(row.year) + '_' + str(row.quarter)
#     if k not in quarter_sales:
#         quarter_sales[k] = 0
#     quarter_sales[k] += row.buy_mount
# print(quarter_sales)
#
# ks = sorted(quarter_sales.keys())
# print(ks)
# vs = [quarter_sales[k] for k in ks]
# plt.figure(figsize=(10, 5))
# plt.bar(ks, vs)
# plt.title("Sales Volume By Year_Quarter")
# plt.xlabel("Year_Quarter")
# plt.ylabel("Sales Volumn")
#
# month_sales = {}  # keys: 年份_季度， value: 销售总量
#
# for index, row in df_trade.iterrows():
#     k = str(row.year) + '_' + str(row.month)
#     if k not in month_sales:
#         month_sales[k] = 0
#     month_sales[k] += row.buy_mount
# print(month_sales)
#
# ks = sorted(month_sales.keys())
# vs = [month_sales[k] for k in ks]
# plt.figure(figsize=(20, 5))
# plt.bar(ks, vs)
# plt.title("Sales Volume By Year_Month")
# plt.xlabel("Year_Month")
# plt.ylabel("Sales Volumn")
#
# df_trade_201411 = df_trade[(df_trade.day >= '2014-11-01') & (
#             df_trade.day <= '2014-11-30')]
# day_sales = {}  # keys: 年份_季度， value: 销售总量
#
# for index, row in df_trade_201411.iterrows():
#     k = row.day.day
#     print(k)
#     if k not in day_sales:
#         day_sales[k] = 0
#     day_sales[k] += row.buy_mount
# print(day_sales)
#
# ks = sorted(day_sales.keys())
# vs = [day_sales[k] for k in ks]
# plt.figure(figsize=(10, 5))
# plt.bar(ks, vs)
# plt.title("Sales Volume By 2014 11")
# plt.xlabel("2014 11 day")
# plt.ylabel("Sales Volumn")
#
plt.show()
